package com.yanqu.road.server.cmd.strongholdwar.eliminate;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.manager.strongholdwar.MammonHelpResult;
import com.yanqu.road.server.pb.CrossStrongholdPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.AgentProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_MAMMON_HELP, desc = "跨服财神助力")
public class CrossMammonHelpCmd extends CrossUnionCommand {
    private int activityId;
    private int type;
    private List<Integer> idList;
    private long userId;
    private BigInteger totalDamage;
    private int attackTimes;
    private UserBaseInfo userBaseInfo;
    private int goodsId;
    private int mammonType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossStrongholdMammonHelpReqMsg req = StrongholdWarProto.CrossStrongholdMammonHelpReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        type = req.getType();
        idList = req.getIdList();
        userId = packet.getPlayerId();
        totalDamage = new BigInteger(req.getDamage());
        attackTimes = req.getAttackTimes();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(req.getUserBaseInfo());
        goodsId = req.getGoodsId();
        mammonType = req.getMammonType();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        MammonHelpResult result = CrossStrongholdMgr.mammonHelp(activityId, userId, userBaseInfo, idList, type, mammonType, totalDamage, attackTimes);
        StrongholdWarProto.CrossStrongholdMammonHelpRespMsg.Builder resp = StrongholdWarProto.CrossStrongholdMammonHelpRespMsg.newBuilder();
        ret = result.getRet();
        resp.setRet(ret);
        if (ret == 0) {
            StrongholdWarProto.CrossStrongholdMammonHelpDataMsg.Builder msg = StrongholdWarProto.CrossStrongholdMammonHelpDataMsg.newBuilder();
            msg.setKillNum(result.getKillNum());
            msg.setAttackUnionNum(result.getAttackNum());
            msg.setTotalDamage(result.getTotalDamage().toString());
            for (Map.Entry<Integer, BigInteger> entry : result.getUnionDamageMap().entrySet()) {
                Integer id = entry.getKey();
                BigInteger damage = entry.getValue();
                StrongholdWarProto.mammonHelpDamageMsg.Builder detail = StrongholdWarProto.mammonHelpDamageMsg.newBuilder();
                detail.setId(id);
                detail.setDamage(damage.toString());
                msg.addDamageDetail(detail);
            }
            msg.setAttackUnionName(result.getUnionName());
            resp.setResult(msg);
            sendMammonHelpNotice();
        }
        resp.setGoodsId(goodsId);
        resp.setMammonType(mammonType);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_STRONGHOLD_MAMMON_HELP_NOTIFY, resp);
        MessageHelper.sendPacket(serverId, userId, pbMsg);

    }

    private void sendMammonHelpNotice() {
        ActivityInfo activityInfo = CrossStrongholdActivityMgr.getOpenActivityInfo(activityId);
        if (activityInfo != null) {
            List<Long> serverIdList = activityInfo.getServerIdList();
            for (Long serverId : serverIdList) {
                StrongholdWarProto.StrongholdNoticeMammonHelpMsg.Builder msg = CrossStrongholdPb.parseMammonHelpNoticeMsg(userBaseInfo, mammonType, userId);
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_STRONGHOLD_MAMMON_HELP_BROADCAST, msg);
                MessageHelper.sendPacket(serverId, 0, pbMsg);
            }
        }
    }

    @Override
    public void sendData() {

    }
}
